<?php

// $Id$

require_once(TANGRA_MAIN_DIR.'filesystem_toolbox/filesystem_functions.inc.php');

require_once($WSC->get_site_inc_dir().'modules/tangra_cms/_sys/tcms_admin_web_page_wed_ua.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/sitemap/tcms_sitemap_generator.class.php');


class Sitemap_Regenerate_WP extends TCMS_Admin_Web_Page_WED_UA {
	public function create_view($name = 'default') {
		$ret = new TCMS_Admin_Page_View($this, $name, 'modules/tangra_cms/pages/_sitemap/');

		return $ret;
	}


	public function init() {
		parent::init();

		$v = $this->create_view();
		$this->add_view($v);

		$v2 = $this->create_view('regenerated');
		$this->add_view($v2);

		$this->wed->add_wed_event_action_pair(new Web_Event_Simple_Int('regenerate_confirmed', Tangra_Parameter_Method::GET, 'regenerate_confirmed'), 'regenerate_confirmed');
	}


	protected function default_event() {
		$view = $this->get_view('default');

		return $view;
	}


	protected function regenerate_confirmed() {
		$view = $this->get_view('regenerated');

		$dbc = $this->get_dbc();
		$tple= new Tple();

		$prefix = $this->prepare_prefix();
		TCMS_Sitemap_Generator::generate($dbc, $tple, $this->_config->get_scratch_dir(), $prefix);

		return $view;
	}


	private function prepare_prefix() {
		$ret = $this->_config->get_current_protocol().'://'.$this->_config->get_current_host();
		if ($this->_config->get_current_port() && $this->_config->get_current_port() != '80') {
			$ret .= ':'.$this->_config->get_current_port();
		}
		$ret .= '/';
		return $ret;
	}
}